/**
  ******************************************************************************
  * @file           : paraupcb.c
  * @brief          : 参数更新回调函数
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "parameter.h"
#include "measure.h"

/* 通用数字输入, 若想细分,可实现通用无符号整数 或 通用浮点数输入等 */
uint8_t sto_inputcb_general_number(const Sto_MaintainStru *pmt, uint8_t cmd, uint8_t key)
{
  uint8_t timeout;
  uint8_t err;
  
  if((pmt == NULL)||(pmt->inputbuf == NULL) || (pmt->inputpos == NULL))
  {
    return 4;
  }

  switch(cmd)
  {
    case STO_MTDINPUT_CMD_INIT:
      pmt->inputbuf[0] = '0';
      pmt->inputbuf[1] = '\0';
      (*(pmt->inputpos)) = 0;
      break;

    case STO_MTDINPUT_CMD_DEL1:
      if(((*(pmt->inputpos)) > 1) && ((*(pmt->inputpos)) < 12)){
        pmt->inputbuf[--(*(pmt->inputpos))]= '\0';
      }else{
        (*(pmt->inputpos)) = 0;
        pmt->inputbuf[0] = '0';
        pmt->inputbuf[1] = '\0';
      }
      return 0;

    case STO_MTDINPUT_CMD_KEYT:
      if((*(pmt->inputpos)) < 11){
        for(uint8_t i=0; i<11; i++)
        {
          if(pmt->inputbuf[i] == '\0'){
            break;
          }

          if(pmt->inputbuf[i] == '.'){
            return 0;
          }
        }

        if((*(pmt->inputpos)) == 0){
          pmt->inputbuf[(*(pmt->inputpos))++] = '-';
        }else{
          pmt->inputbuf[(*(pmt->inputpos))++] = '.';
        }
      
        pmt->inputbuf[(*(pmt->inputpos))] = '\0';
      }
      return 0;

    case STO_MTDINPUT_CMD_KEYOK:
      err = pmt->dynstr(pmt, STO_MTDYNSTR_INPUT_VALUE, (char *)pmt->inputbuf, 12);
      if(err != 0){
        if(err == STO_DO_NOT_SAVE_NOERR){
          return 1;
        }
        return 2;
      }

      timeout = 200;
      while(timeout--)
      {
        vTaskDelay(1);
        if((sto_save_feedback & 0x00FFu) == pmt->idx){
          err = (uint8_t)(sto_save_feedback >> 8);
          if(err != 0){
            return (err+5);
          }else{
            return 0;
          }
        }
      }
      return 3;

    case STO_MTDINPUT_CMD_KEY:
      if((key >=0) && (key <= 9)){
        if((*(pmt->inputpos)) < 11){
          pmt->inputbuf[(*(pmt->inputpos))++] = key+'0';
          pmt->inputbuf[(*(pmt->inputpos))] = '\0';
        }
      }
      return 0;

    default:
      return 5;
  }

  return 5;
}

uint8_t sto_inputcb_general_ip(const Sto_MaintainStru *pmt, uint8_t cmd, uint8_t key)
{
  uint8_t temp;
  uint8_t timeout;
  uint8_t err;
  
  if((pmt == NULL)||(pmt->inputbuf == NULL) || (pmt->inputpos == NULL))
  {
    return 4;
  }

  switch(cmd)
  {
    case STO_MTDINPUT_CMD_INIT:
      pmt->inputbuf[0] = '0';
      pmt->inputbuf[1] = '\0';
      (*(pmt->inputpos)) = 0;
      break;

    case STO_MTDINPUT_CMD_DEL1:
      if(((*(pmt->inputpos)) > 1) && ((*(pmt->inputpos)) < 15)){
        pmt->inputbuf[--(*(pmt->inputpos))]= '\0';
      }else{
        (*(pmt->inputpos)) = 0;
        pmt->inputbuf[0] = '0';
        pmt->inputbuf[1] = '\0';
      }
      return 0;

    case STO_MTDINPUT_CMD_KEYT:
      if((*(pmt->inputpos)) < 15){
        temp = 0;
        for(uint8_t i=0; i<15; i++)
        {
          if(pmt->inputbuf[i] == '\0'){
            break;
          }
          
          if(pmt->inputbuf[i] == '.'){
            if(++temp >= 3){
              return 0;
            }
          }
        }

        if((*(pmt->inputpos)) != 0){
          pmt->inputbuf[(*(pmt->inputpos))++] = '.';
        }
        
        pmt->inputbuf[(*(pmt->inputpos))] = '\0';
      }
      return 0;

    case STO_MTDINPUT_CMD_KEYOK:
      err = pmt->dynstr(pmt, STO_MTDYNSTR_INPUT_VALUE, (char *)pmt->inputbuf, 15);
      if(err != 0){
        if(err == STO_DO_NOT_SAVE_NOERR){
          return 1;
        }
        return 2;
      }

      timeout = 200;
      while(timeout--)
      {
        vTaskDelay(1);
        if((sto_save_feedback & 0x00FFu) == pmt->idx){
          err = (uint8_t)(sto_save_feedback >> 8);
          if(err != 0){
            return (err+5);
          }else{
            return 0;
          }
        }
      }
      return 3;

    case STO_MTDINPUT_CMD_KEY:
      if((key >=0) && (key <= 9)){
        if((*(pmt->inputpos)) < 15){
          pmt->inputbuf[(*(pmt->inputpos))++] = key+'0';
          pmt->inputbuf[(*(pmt->inputpos))] = '\0';
        }
      }
      return 0;

    default:
      return 5;
  }

  return 5;
}

